package com.group525.cloudef.interceptor;

import com.group525.cloudef.utils.JwtUtil;

import io.jsonwebtoken.Claims;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;


import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.Date;
import java.util.Optional;


public class MyInterceptor implements HandlerInterceptor {
    @Resource
    JwtUtil jwtUtil;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        System.out.println(">>>>>>>>>>>>>>>>>>>>拦截器启动了");
//        Optional<String> stringOptional = Optional.ofNullable(request.getHeader("token"));
//         if (stringOptional.isPresent()){
//             System.out.println(">>>>>>>>>>>>>token存在");
//             String token = stringOptional.get();
//             Claims claims = jwtUtil.parseJWT(token);
//             Date expiration = claims.getExpiration();
//             System.out.println(expiration);
//             Date date=new Date();
//             return date.after(expiration);
//         }else {
//             return false;
//         }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
